package logic

import (
	"context"
	fmt2 "fmt"
	"gorm.io/gorm"
	"product/model/mysql"

	"product/productrpc/internal/svc"
	"product/productrpc/productrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateLogic) Update(in *productrpc.UpdateRequest) (*productrpc.UpdateResponse, error) {
	// todo: add your logic here and delete this line

	Update := mysql.Product{
		Model:   gorm.Model{ID: uint(in.Id)},
		Product: in.Product,
		Img:     in.Img,
		Status:  in.Status,
	}

	if Update.ID == 0 {
		return nil, fmt2.Errorf("账号不存在,请重新输入")
	}
	mysql.DB.Debug().Model(&Update).Updates(&Update)

	return &productrpc.UpdateResponse{Id: int64(Update.ID)}, nil
}
